package com.jsoft.controller;

import com.alibaba.fastjson.JSON;
import com.jsoft.entity.Enterprise;
import com.jsoft.entity.TicketOpen;
import com.jsoft.entity.User;
import com.jsoft.entity.po.TicketEnterInsti;
import com.jsoft.service.EnterpriseService;
import com.jsoft.service.TicketEnterInstiService;
import com.jsoft.service.UserService;
import com.jsoft.service.impl.EnterpriseServiceImpl;
import com.jsoft.service.impl.TicketEnterInsiServiceImpl;
import com.jsoft.service.impl.UserServiceImpl;
import com.jsoft.utils.ResultUtil;
import com.mysql.jdbc.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName TicketEnterInstiController
 * @Description TODO
 * @Author jindong
 * @Date 2023/2/23 0:14
 * @Version 1.0
 */
@WebServlet("*.te")
@MultipartConfig
public class TicketEnterInstiController extends HttpServlet {
    private TicketEnterInstiService ticketEnterInstiService = new TicketEnterInsiServiceImpl();

    /**
     * 接受请求并拆分出其方法名 调用反射执行其方法
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String servletPath = req.getServletPath();
        String methodName = servletPath.substring(servletPath.lastIndexOf("/") + 1, servletPath.length() - 3);
        try {
            Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, req, resp);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询复核列表的数据
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void showAllTicketEnterInsti(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        List<TicketEnterInsti> ticketEnterInstis = ticketEnterInstiService.selectAllTicketEnterInsti();
        writer.write(JSON.toJSONString(new ResultUtil("TicketEnterInstiList", ticketEnterInstis)));
        writer.flush();
        writer.close();
    }


    /**
     * 点复核按钮展示其对应的所有信息
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void showTicketEnterInstiById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        Integer id = Integer.parseInt(req.getParameter("id"));
        TicketEnterInsti ticketEnterInstiById = ticketEnterInstiService.findTicketEnterInstiById(id);
        writer.write(JSON.toJSONString(new ResultUtil("ticketEnterInstiById", ticketEnterInstiById)));
        writer.close();
    }

    /**
     * 点击驳回触发的方法 将其状态改成A(成功)
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void updateStatus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        String tid = req.getParameter("tid");
        int id = Integer.parseInt(tid);
        System.out.println(tid);
        int res = ticketEnterInstiService.updateStatusById(id);
        System.out.println(res);
        if (res > 0) {
            writer.write(JSON.toJSONString(new ResultUtil(300, "复核成功")));
        } else {
            writer.write(JSON.toJSONString(new ResultUtil("301", "复核失败")));
        }

    }

    /**
     * 点击驳回触发的方法 将其状态改成D(复核未通过)
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void changeStatus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("*********");
        PrintWriter writer = resp.getWriter();
        String tid = req.getParameter("tid");
        int id = Integer.parseInt(tid);
        System.out.println(id);
        int res = ticketEnterInstiService.changeStatusById(id);
        if (res > 0) {
            writer.write(JSON.toJSONString(new ResultUtil(302, "驳回成功")));
        } else {
            writer.write(JSON.toJSONString(new ResultUtil(303, "复核失败")));
        }
    }

    /**
     * 将查询的条件封装为一个对象的属性
     * 根据其属性定向查询
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void CheckTicketByView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        //读取凭证编号
        String nostr = request.getParameter("no");
        //读取收单企业
        String enterpriseName = request.getParameter("enterpriseName");
        //开单企业
        String acquirerName = request.getParameter("acquirerName");
        //最低金额
        String minamount = request.getParameter("minamount");
        //最高金额
        String maxamount = request.getParameter("maxamount");
        //开单时间
        String createTime = request.getParameter("createTime");

        System.out.println(nostr);
        /**
         * 判断是否为空 不为空添加到该对象中
         */
        TicketEnterInsti ticketEnterInsti = new TicketEnterInsti();
        if (!StringUtils.isNullOrEmpty(nostr)) {
            ticketEnterInsti.setNo(nostr);
        }
        if (!StringUtils.isNullOrEmpty(enterpriseName)) {
            ticketEnterInsti.setEnterpriseName(enterpriseName);
        }
        if (!StringUtils.isNullOrEmpty(acquirerName)) {
            ticketEnterInsti.setAcquirerName(acquirerName);
        }
        if (!StringUtils.isNullOrEmpty(minamount)) {
            ticketEnterInsti.setMinamount(minamount);
        }
        if (!StringUtils.isNullOrEmpty(maxamount)) {
            ticketEnterInsti.setMaxamount(maxamount);
        }
        /**
         * createTime的类型转换
         */
        if (!StringUtils.isNullOrEmpty(createTime)) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date format = null;
            try {
                format = simpleDateFormat.parse(createTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            ticketEnterInsti.setCreateTime(format);
        }
        /**
         * 返回给前端查询到的符合条件的列表
         */
        try {
            List<TicketEnterInsti> viewList = ticketEnterInstiService.CheckTicketByView(ticketEnterInsti);
            out.write(JSON.toJSONString(new ResultUtil("viewlists", viewList)));
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            out.close();
        }
    }


    /**
     * 根据Account查询付款列表
     * @param req 请求
     * @param resp 响应
     * @throws ServletException servl异常
     * @throws IOException IO流异常
     */
    private void findOpenList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        BufferedReader reader = req.getReader();
        String json = reader.readLine();
        System.out.println(json);



        EnterpriseService enterpriseService = new EnterpriseServiceImpl();
        List<TicketEnterInsti> openList = null;
        try {
            openList = ticketEnterInstiService.findOpenList();
            out.write(JSON.toJSONString(new ResultUtil("openList",openList)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 根据no回显凭证详情
     * @param req 请求
     * @param resp 响应
     * @throws ServletException servlet异常
     * @throws IOException IO流异常
     */
    private void findDetailByNo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        PrintWriter out = resp.getWriter();

        BufferedReader reader = req.getReader();
        String json = reader.readLine();
        TicketEnterInsti ticketEnterInsti = JSON.parseObject(json,TicketEnterInsti.class);
        String no = ticketEnterInsti.getNo();
        System.out.println(ticketEnterInsti.getNo());

        try {
            TicketEnterInsti insti = ticketEnterInstiService.findDetailByNode(no);
            System.out.println(insti);

            out.write(JSON.toJSONString(new ResultUtil("insti",insti)));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            out.close();
        }
    }

    /**
     * 点击撤销触发得事件方法
     * 将选中数据得状态改为C(已撤销)
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void exchangeStatusByNo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        BufferedReader reader = req.getReader();
        String json = reader.readLine();
        TicketEnterInsti ticketEnterInsti = JSON.parseObject(json, TicketEnterInsti.class);
        String no = ticketEnterInsti.getNo();


        int res = ticketEnterInstiService.updateStatusByNo(no);
        System.out.println(res);
        if(res>0){
            writer.write(JSON.toJSONString(new ResultUtil("500","撤销成功")));
        }else{
            writer.write(JSON.toJSONString(new ResultUtil("501","撤销失败")));
        }
    }


    public void selectOpenList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        PrintWriter out = resp.getWriter();
        //读取凭证编号
        String nostr = req.getParameter("no");
        //读取开单企业
        String enterpriseName = req.getParameter("enterpriseName");
        System.out.println(enterpriseName);
        //收单企业
        String acquirerName = req.getParameter("acquirerName");
        //最低金额
        String minamount = req.getParameter("minamount");
        //最高金额
        String maxamount = req.getParameter("maxamount");
        //开单时间
        String createTime = req.getParameter("createTime");
        /**
         * 判断是否为空 不为空添加到该对象中
         */
        TicketEnterInsti ticketEnterInsti = new TicketEnterInsti();
        if (!StringUtils.isNullOrEmpty(nostr)) {
            ticketEnterInsti.setNo(nostr);
        }
        if (!StringUtils.isNullOrEmpty(enterpriseName)) {
            ticketEnterInsti.setEnterpriseName(enterpriseName);
        }
        if (!StringUtils.isNullOrEmpty(acquirerName)) {
            ticketEnterInsti.setAcquirerName(acquirerName);
        }
        if (!StringUtils.isNullOrEmpty(minamount)) {
            ticketEnterInsti.setMinamount(minamount);
        }
        if (!StringUtils.isNullOrEmpty(maxamount)) {
            ticketEnterInsti.setMaxamount(maxamount);
        }
        /**
         * createTime的类型转换
         */
        if (!StringUtils.isNullOrEmpty(createTime)) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date format = null;
            try {
                format = simpleDateFormat.parse(createTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            ticketEnterInsti.setCreateTime(format);
        }
        /**
         * 返回给前端查询到的符合条件的列表
         */
        try {
            List<TicketEnterInsti> viewList = ticketEnterInstiService.selectViewByCondition(ticketEnterInsti);
            System.out.println(viewList);
            out.write(JSON.toJSONString(new ResultUtil("viewlists", viewList)));
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            out.close();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
